package com.nl.controller.web.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.SmsReportTotal;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.mapper.SmsReportTotalMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class SmsReportTotalService {

    @Autowired
    private SmsReportTotalMapper smsReportTotalMapper;

    @Autowired
    private CommonService commonService;

    @SystemServiceLog(description = "短信发送量统计分析查询", value = false)
    public PageInfo<SmsReportTotal> find
            (SmsUsers smsUsers, int page, int limit, String month, String way, String startTime, String endTime, String countWay, String cityId, String mgrId, String mgrName, String regionId, String stationId, String countyId) {
        List<SmsReportTotal> list = new ArrayList<>();
        PageHelper.startPage(page, limit);
        try {
            list = this.getDataList(smsUsers, way, month, startTime, endTime, countWay,cityId,mgrId,mgrName,regionId,stationId,countyId);
            commonService.createSmsOperLog("4", "短信发送量统计查询", "0", "短信发送统计分析");
        } catch (Exception e) {
            commonService.createSmsOperLog("4", "短信发送量统计查询", "1", "短信发送统计分析");
            throw new SmsException("短信发送量统计分析查询失败");
        }
        PageInfo<SmsReportTotal> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @SystemServiceLog(description = "短信发送量统计明细导出", value = false)
    public JSONArray download
            (SmsUsers smsUsers, String way, String month, String startTime, String endTime, String countWay,String cityId,String mgrId,String mgrName,String regionId,String stationId,String countyId) {
        {
            List<SmsReportTotal> list = new ArrayList<>();
            try {
                list = this.getDataList(smsUsers, way, month, startTime, endTime, countWay,cityId,mgrId,mgrName,regionId,stationId,countyId);
                commonService.createSmsOperLog("4", "短信发送量统计导出", "0", "短信发送统计分析");
            } catch (Exception e) {
                commonService.createSmsOperLog("4", "短信发送量统计导出", "1", "短信发送统计分析");
                throw new SmsException("短信发送量统计分析导出失败");
            }
            JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(list));
            return jsonArray;
        }
    }

    @SystemServiceLog(description = "短信发送量统计查询数据获取", value = false)
    private List<SmsReportTotal> getDataList(SmsUsers smsUsers, String way, String month, String startTime, String endTime, String countWay,String cityId,String mgrId,String mgrName,String regionId,String stationId,String countyId) {
        List<SmsReportTotal> list = null;
        String duty = smsUsers.getDuty();
        Map<String, Object> map = new HashMap<>();
        //地市管理员限制
        if ("1".equals(duty)) {
            map.put("cityId", smsUsers.getCityId());
            //项目管理员限制
        } else if ("2".equals(duty)) {
            map.put("mgrIdList", smsUsers.getMgrId().split(","));
            
        }
        if ("0".equals(way)) {
            map.put("month", month);
        } else {
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        }
        try {
            if ("city".equals(countWay)) {
                log.info("根据地市查询短信发送统计");
                list = smsReportTotalMapper.findByCity(map);
            } else if (("mgr").equals(countWay)) {
                log.info("根据项目查询发送统计");
                map.put("cityId",cityId);
                map.put("mgrId",mgrId);
                map.put("mgrName",mgrName);
                map.put("countyId",countyId);
                list = smsReportTotalMapper.findByMgr(map);
            } else if (("region").equals(countWay)) {
                map.put("cityId",cityId);
                map.put("regionId",regionId);
                log.info("根据区域查询发送统计");
                map.put("mgrId",mgrId);
                map.put("countyId",countyId);
                list = smsReportTotalMapper.findByRegion(map);
            } else if(("Station").equals(countWay)){
                map.put("cityId",cityId);
                map.put("mgrId",mgrId);
                map.put("regionId",regionId);
                map.put("stationId",stationId);
                map.put("countyId",countyId);
                log.info("根据基站查询发送统计");
                list = smsReportTotalMapper.findByStation(map);
            } else if(("county").equals(countWay)){
                map.put("cityId",cityId);
                map.put("mgrId",mgrId);
                map.put("regionId",regionId);
                map.put("stationId",stationId);
                map.put("countyId",countyId);
                log.info("根据区县查询发送统计");
                list = smsReportTotalMapper.findByCounty(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信发送量统计数据获取出现异常：" + countWay);
            throw e;
        }
        return list;
    }
}




